
// ==================== src/utils/notification.ts ====================
/**
 * 系统通知工具函数
 */

// 请求通知权限
export const requestNotificationPermission = async (): Promise<boolean> => {
  if (!('Notification' in window)) {
    console.warn('This browser does not support desktop notification');
    return false;
  }

  if (Notification.permission === 'granted') {
    return true;
  }

  if (Notification.permission !== 'denied') {
    const permission = await Notification.requestPermission();
    return permission === 'granted';
  }

  return false;
};

// 显示系统通知
export const showSystemNotification = (
  title: string,
  body: string,
  options?: NotificationOptions
): Notification | null => {
  if ('Notification' in window && Notification.permission === 'granted') {
    return new Notification(title, {
      body,
      icon: '/logo.png',
      badge: '/logo.png',
      ...options,
    });
  }
  return null;
};

// 虚拟机状态变化通知
export const notifyVMStateChange = (vmName: string, state: string) => {
  const stateMessages: Record<string, string> = {
    running: '已启动',
    shutoff: '已关机',
    paused: '已暂停',
    crashed: '已崩溃',
  };

  const message = stateMessages[state] || state;
  showSystemNotification(
    '虚拟机状态变化',
    `${vmName} ${message}`,
    {
      tag: `vm-${vmName}`,
      requireInteraction: false,
    }
  );
};